import { Component, OnInit, Input, Optional, Host } from '@angular/core';

import AttributionLike from 'ol/attribution';
import SourceOSM from 'ol/source/osm';
import proj from 'ol/proj';

import { TileComponent as LayerTileComponent } from '../../layer/tile/tile.component';

@Component({
  selector: 'ngx-ol-source-osm',
  templateUrl: './osm.component.html',
  styleUrls: ['./osm.component.scss']
})
export class OsmComponent implements OnInit {

  public instance: SourceOSM;

  @Input() attributions: AttributionLike;
  @Input() cacheSize: number;
  @Input() crossOrigin: null|string;
  @Input() maxZoom: number;
  @Input() opaque: boolean;
  @Input() reprojectionErrorThreshold: number;
  @Input() url: string;
  @Input() wrapX: boolean;

  host: any;

  constructor(
    @Optional() @Host() layerTileComponent: LayerTileComponent
  ) { 
    if(!!layerTileComponent) {
      this.host = layerTileComponent;
    }
    if(!this.host) {
      throw Error('ngx-ol-source-osm must be a descendant of <ngx-ol-layer-tile>');
    }
  }

  ngOnInit() {
    this.instance = new SourceOSM(this);
    this.host.setSource(this.instance);
  }

}
